package com.fenxiangle.yueding.common.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.GenreClassBo;
import com.fenxiangle.yueding.entity.bo.GenreSeedBo;
import com.fenxiangle.yueding.entity.bo.OccupationBo;
import com.fenxiangle.yueding.feature.mine.view.adapter.RequirementGenreSendAdapter;

import java.util.List;

/**
 * Created by Home on 2018/9/3.
 */

public class PublishGenrePopupWindow implements PopupWindow.OnDismissListener {
    Callback callback;
    @Override
    public void onDismiss() {
        backgroundAlpha(1f);
    }

    LayoutInflater layoutInflater;
    private Activity mActivity;

    public PublishGenrePopupWindow(Activity activity) {
        this.mActivity = activity;
        layoutInflater = LayoutInflater.from(activity);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

    PopupWindow popupWindow;

    public void initPopupWindow(View view, List<OccupationBo.SecondBean> getGenreSeedBos) {

        View popupWindowView = layoutInflater.inflate(R.layout.activity_requirement_genre, null);
        //内容，高度，宽度
        popupWindow = new PopupWindow(popupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT, true);
//        popupWindow.setAnimationStyle(R.style.AnimationRightFade);
        //菜单背景色
//        ColorDrawable dw = new ColorDrawable(0xffffffff);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //宽度
        //popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
        //高度
        //popupWindow.setHeight(LayoutParams.FILL_PARENT);
        //显示位置

        //设置背景半透明
        backgroundAlpha(0.5f);
        //关闭事件
        popupWindow.setOnDismissListener(this);
        popupWindow.setWidth(getScreenWidth(mActivity)-300);
        RecyclerView genre_send = popupWindowView.findViewById(R.id.genre_send);
        genre_send.setLayoutManager(new GridLayoutManager(mActivity, 3));
        RequirementGenreSendAdapter genreSendAdapter = new RequirementGenreSendAdapter(getGenreSeedBos);
        genre_send.setAdapter(genreSendAdapter);
        genreSendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                genreSendAdapter.setSelectItem(position);
                callback.getStr(genreSendAdapter.getData().get(position).getOccupation());
                genreSendAdapter.notifyDataSetChanged();
            }
        });
        popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 500);
        popupWindowView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                return false;
            }
        });
    }
     /**
     * 获取屏幕的高度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    public interface  Callback{
        void getStr(String str);
    }

    public void setCallback(Callback callback){
        this.callback=callback;
    }
}
